package megrez.boot.autoconfigure.xxljob;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;

import lombok.extern.slf4j.Slf4j;

/**
 * xxl-job自动配置
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ XxlJobSpringExecutor.class })
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobAutoConfiguration {
    
    @Bean
    @ConditionalOnMissingBean
    public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties properties) {
        log.info("Initializing XxlJobSpringExecutor");

        XxlJobSpringExecutor rslt = new XxlJobSpringExecutor();
        rslt.setAdminAddresses(properties.getAdmin().getAddresses());
        rslt.setAppname(properties.getExecutor().getAppname());
        rslt.setAddress(properties.getExecutor().getAddress());
        rslt.setIp(properties.getExecutor().getIp());
        rslt.setPort(properties.getExecutor().getPort());
        rslt.setAccessToken(properties.getAccessToken());
        rslt.setLogPath(properties.getExecutor().getLogpath());
        rslt.setLogRetentionDays(properties.getExecutor().getLogretentiondays());
        return rslt;
    }
}
